﻿using System;
using System.Windows;
using System.Windows.Media;
using TicTacToe.Core;
using _3DTicTacToe.UI;

namespace _3DTicTacToe.UI
{
    partial class GameOverWindow : Window
    {
        private readonly TicTacToeGame _game;

        public GameOverWindow()
        {
            InitializeComponent();
        }

        public GameOverWindow(GameOverEventArgs args, TicTacToeGame game, bool isNetGame)
        {
            InitializeComponent();

            _game = game;

            if (isNetGame)
            {
                rematchButton.Visibility = Visibility.Hidden;
                rematchButton.IsEnabled = false;
            }

            if (args.IsDraw)
                drawGrid.Visibility = Visibility.Visible;
            else
            {
                winGrid.Visibility = Visibility.Visible;

                winningPlayerNameRun.Foreground = new SolidColorBrush(args.WinningPlayer.Color);
                losingPlayerNameRun.Foreground = new SolidColorBrush(args.LosingPlayer.Color);

                winningPlayerNameRun.Text = args.WinningPlayer.Name + " ";
                losingPlayerNameRun.Text = args.LosingPlayer.Name + " ";

                var coords = args.GetWinningCoordinates();

                firstCP.Content = string.Format("({0} {1} {2})", coords[0], coords[1],
                                                coords[2]);
                secondCP.Content = string.Format("({0} {1} {2})", coords[3], coords[4],
                                                 coords[5]);
                thirdCP.Content = string.Format("({0} {1} {2})", coords[6], coords[7],
                                                coords[8]);
                fourthCP.Content = string.Format("({0} {1} {2})", coords[9], coords[10],
                                                 coords[11]);
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            new StartWindow().Show();

            Owner.Close();
            Close();
        }

        private void rematchButton_Click(object sender, RoutedEventArgs e)
        {
            new GameWindow(new TicTacToeGame(_game.PlayerTwo, _game.PlayerOne)).Show(); //exchange players

            Owner.Close();
            Close();
        }
    }
}
